<template>
<label class="el-checkbox-button el-checkbox-button--medium" :class="{'is-checked': isChecked()}">
    <input
    type="checkbox"
      v-model="inputCheckbox"
      :name="name"
      style="display:none"
      @change="handleChange"
      @focus="focus=true"
      @blur="focus=false" />
    <span class="el-checkbox-button__inner">
      <slot>{{label}}</slot>
    </span>
</label>
</template>

<script>
export default {
  name: 'CheckboxButton',
  data() {
    return {
      inputCheckbox: false
    };
  },
  props: {
    value: Array,
    name: String,
    attrValue: {},
    label: {}
  },
  computed: {
    inpuValue: {
      get: function() {
        return this.value;
      },
      set: function(val) {
        this.$emit('input', val);
      }
    }
  },
  created() {
    // console.log(this.label)
  },
  methods: {
    isChecked() {
      var _checked = this.inpuValue && this.inpuValue.findIndex(_item => _item._id === this.label._id) !== -1;
      this.inputCheckbox = _checked;
      return _checked;
    },
    handleChange(ev) {
      if (ev.target.checked) {
        if (!this.isChecked()) {
          this.inpuValue.push(this.label);
        }
      } else {
        this.inpuValue.splice(this.inpuValue.findIndex(_item => _item._id === this.label._id), 1);
      }
      this.$emit('change', this.attrValue);
    }
  }
};
</script>

<style scoped>
</style>
